package com.org.springsecurityoauth2.service.impl;

import com.org.springsecurityoauth2.config.BcryPasswordEncoder;
import com.org.springsecurityoauth2.entity.UserDemo;
import com.org.springsecurityoauth2.service.SecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Service
public class SecurityServiceImpl implements SecurityService  {
  public static String username ="admin";
  public static String password ="123456";
    @Autowired
    BcryPasswordEncoder passwordEncoder;

    @Override
    public UserDetails getUserDetails() {
        return null;
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        String encode = passwordEncoder.encode(password);
        Set<GrantedAuthority> set = new HashSet<>();
        GrantedAuthority grantedAuthority1= new SimpleGrantedAuthority("add");
        GrantedAuthority grantedAuthority2 = new SimpleGrantedAuthority("select");
        set.add(grantedAuthority1);
        set.add(grantedAuthority2);
        List<GrantedAuthority> delete = AuthorityUtils.commaSeparatedStringToAuthorityList("delete");
        UserDemo userDemo = new UserDemo(username,encode ,set);
        return userDemo;
    }
}
